﻿using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.DeptTreeCommand;
using B.S.Data.MES.API.Dto.TreeDTo;
using B.S.Domain.Accounts;
using B.S.Infratrctrue;
using B.S.Requst;
using Hangfire.MySql.src.Entities;
using MediatR;

namespace B.S.Data.MES.API.Applications.Management.SteamApplication.CommandHandler.DeptTreeCommandHandler
{
    public class DeptTreesCommandHandler : IRequestHandler<DeptTreesCommand, APIRequst<List<TreeDTOs>>>
    {
        public DeptTreesCommandHandler(IBaseRepository<DeptClass> deptClassRepository)
        {
            DeptClassRepository = deptClassRepository;
        }

        public IBaseRepository<DeptClass> DeptClassRepository { get; }

        public async  Task<APIRequst<List<TreeDTOs>>> Handle(DeptTreesCommand request, CancellationToken cancellationToken)
        {

            return new APIRequst<List<TreeDTOs>>
            {
                Code = RequstCode.成功,
                Msg = "",
                Data = GetDeptTrees(0)
            };
        }

        public  List<TreeDTOs> GetDeptTrees(int id)
        {
            var path = DeptClassRepository.GetAll().Where(a => a.DeptProId == id).ToList();
            List<TreeDTOs> treeDTOs = new List<TreeDTOs>();
            foreach (var item in path)
            {
                TreeDTOs treeDTO = new TreeDTOs()
                {
                    value = item.DeptId,
                    label = item.DeptName,
                    children = GetDeptTrees(item.DeptId)
                };
                treeDTOs.Add(treeDTO);
            }
            return treeDTOs;

        }
    }
}
